package com.patch.activity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import viin.patch.update.PatchUpdate;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;

/**
 * 
 * @author karry
 *jni里面是c实现把补丁和旧版本合成为新版本代码
 *assets里面androidPatchc.rar是补丁的一些工具和代码.
 *具体使用,请看我的新浪博客.
 *http://blog.sina.com.cn/s/blog_a6fb4d820101arcv.html
 */
public class MainActivity extends Activity {
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        String	sd	=	Environment.getExternalStorageDirectory()	+	"/1/";

        File	file	=	new	File(sd);
        if(file.exists()){
        	if(!file.isDirectory()){
        		file.delete();
        		file.mkdir();
        	}
        }else {
			file.mkdir();
		}
        String	patch	=	"app.patch";
        String	oldapk	=	"old.apk";
        
        String oldapk_filepath =	sd	+	oldapk;
        String newapk_savepath =	sd	+	"new.apk";
        String patchpath =	sd	+	patch;
        
        /**
         * 由于assets里面放置的文件可以很大,但读取单个文件超过1M的时候,就会报Data exceeds UNCOMPRESS_DATA_MAX (1314625 vs 1048576)错误.
         * 在此,我把app.patch分包为每个大小为1M的文件,app.patch分包为3个文件,如app1.patch,app2.patch....
         * old.apk没有超过1M,暂时没有分包
         * 分包代码在本页未尾由注释代码给出
         */
        
        AssetManager am = getAssets();
        //先把assets里面patch文件写入sd的1目录下面
        try {
        	//String[]	patches	=	new	String[]{"app1.patch","app2.patch","app3.patch"};  
         	String[]	patches	=	new	String[]{"app1.patch"}; 
			InputStream is = null;
			byte[]	buffer	=	new	byte[1024];
			int	tmp	=	-1;
			
			OutputStream	outputStream	=	new	FileOutputStream(new	File(patchpath));
			
			for (int i = 0; i < patches.length; i++) {
				is	=	am.open(patches[i]);
				while((tmp	=	is.read(buffer))	!=	-1){
					outputStream.write(buffer, 0, tmp);	
				}
				outputStream.flush();
				is.close();
			}
			outputStream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
        
      //先把assets里面old apk文件写入sd的1目录下面
        try {
			InputStream is = am.open(oldapk);
			byte[]	buffer	=	new	byte[1024	*	2];
			int	tmp	=	-1;
			OutputStream	outputStream	=	new	FileOutputStream(new	File(oldapk_filepath));
			while((tmp	=	is.read(buffer))	!=	-1){
				outputStream.write(buffer, 0, tmp);
			}
			outputStream.flush();
			is.close();
			outputStream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        //把旧的apk和补丁合在一起,就结合成最新版本的apk
        PatchUpdate	patchInterence	=	new	PatchUpdate();
        patchInterence.patch(oldapk_filepath, newapk_savepath, patchpath);
        System.out.println("-------------------------------------------:"+sd);
        Toast.makeText(MainActivity.this, "新的apk已经写入到"	+	sd	+	"目录下面", Toast.LENGTH_LONG).show();
    }
}

/***
 * 分包代码
  	public static void main(String[] args) throws Exception{
		
		// 大文件放置的路径  
	    String path = "/home/karry/temp/";  
	    // 大文件的文件名称  
	    String base = "old";  
	    String ext = ".apk";  
	    // 以每个小文件1024*1024字节即1M的标准来分割  
	    int split = 1024 * 1024;  
	    byte[] buf = new byte[1024];  
	    int num = 1;  
	    // 建立输入流  
	    File inFile = new File(path + base + ext);  
	    FileInputStream fis = new FileInputStream(inFile);  
	    while (true) {  
	        // 以"demo"+num+".db"方式来命名小文件即分割后为demo1.db，demo2.db，。。。。。。  
	        FileOutputStream fos = new FileOutputStream(new File(path + base  
	                + num + ext));  
	        for (int i = 0; i < split / buf.length; i++) {  
	            int read = fis.read(buf);  
	            fos.write(buf, 0, read);  
	            // 判断大文件读取是否结束  
	            if (read < buf.length) {  
	                fis.close();  
	                fos.close();  
	                return;  
	            }  
	        }  
	        fos.close();  
	        num++;
	    }
 */
